package org.example.demo3.demos.web.Controller;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.RequiredArgsConstructor;
import org.example.demo3.demos.web.Mapper.UserMapper;
//import org.example.demo3.demos.web.domain.vo.UserVO;
import org.example.demo3.demos.web.domain.vo.UserVO;
import org.example.demo3.demos.web.entity.User;
import org.example.demo3.demos.web.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RequiredArgsConstructor // 引入必备的参数
@RestController
@Api(value = "接口最外层的描述",tags = "用户接口二层描述")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    private final IUserService iUserService; // 不再用@Autowired了，而是使用final就成为常量，并且@RequiredArgsConstructor 成为必备的参数。

    @RequestMapping("/user22")
    public void insert(){
        User user = new User();
        user.setUsername("姓名");
        user.setPassword(123);
        user.setNickname("昵称");
        user.setEmail("邮箱");
        user.setUser_pic("图片地址");
        user.setCreateTime("2024-1-26 14:21:40");
        user.setUpdateTime("2024-1-26 14:21:46");
        System.out.println(user);

        System.out.println(userMapper.insert(user));
    }


    @GetMapping("/getByid")
    User getByid(@RequestParam(value = "id") String id){
        System.out.println(id);
        User byId = iUserService.getById(id);
         System.out.println(byId);
        return byId;
    }


    @GetMapping("/delUser")
    void delUser(@RequestParam String id)    {
        // 调用Iservice
        boolean save = iUserService.removeById(id);
    }



    @GetMapping("/getUserList")
    List<User> getUserList(@RequestParam(value = "ids") List<String> ids){
        return   iUserService.listByIds(ids);
    }


    //    最复杂的， 根据id扣除余额。 1.判断用户是否冻结 2. 判断用户余额是否够。 然后扣除
    @GetMapping("/UserMinuteYuE")
    void UserMinuteYuE(@RequestParam(value = "id") String id,@RequestParam(value = "money") int money)    {
        // 调用Iservice
        iUserService.UserMinuteYuE(id,money);
    }


    @GetMapping("/duoTiaoJianSearch")
    public List<User> duoTiaoJianSearch(@RequestParam(value = "name") String name, @RequestParam(value = "status") int status,@RequestParam int minPassword,@RequestParam int maxPassword){
        return iUserService.duoTiaoJianFn(name,status,minPassword,maxPassword);
    }

    @ApiModelProperty("介绍接口-根据id和money参数修改指定值")
    @GetMapping("/lambdaUpdateSelf")
    public List<User> lambdaUpdate(@RequestParam(value = "id") String id, @RequestParam(value = "money") Integer money){
        return iUserService.lambdaUpdateSelf(id,money);
    }

    @ApiModelProperty("介绍接口-1w次循环")
    @GetMapping("/tenThousandFor")
    public void tenThousandFor(){
        // 这个是1w次请求
        long first = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            User user =  buildUser(i+20);
            iUserService.save(user);
        }
        long end = System.currentTimeMillis();
        System.out.println("耗时:" + (end - first));
        // 这个是1k * 10次
/*        long first = System.currentTimeMillis();
        List<User> arr = new ArrayList<>(1000);
        for (int i = 0; i < 10000; i++) {
            User user =  buildUser(i+20);
            arr.add(user);
            if(arr.size() % 1000 == 0){
                iUserService.saveBatch(arr);
                arr.clear();
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("耗时:" + (first - end ) );*/
    }


    public User buildUser(int i){
        User user = new User();
        user.setId(i+"");
        user.setStatus(1);
        user.setUpdateTime("2024-1-29 14:55:38");
        user.setEmail("邮箱"+i);
        user.setPassword(333 + i);
        user.setUser_pic(i+"");
        user.setUsername("姓名" + i);
        user.setNickname("www" +i);
        user.setCreateTime("2024-1-29 14:57:11");
        return user;
    }


    // 根据id查询信息，并且需要address的值
    @GetMapping("/getUserByid")
    UserVO getUserByid(@RequestParam String id)    {



        return iUserService.queryUserAndAddress(id);
    }
}




































